<?php
class AdminController extends Zend_Controller_Action
{

    public function init()
    {
       
    }

    public function indexAction()
    {
        $auth = Zend_Auth::getInstance();
		if (!$auth->hasIdentity()) {
			if ($this->_request->getActionName() != 'login') {
				$this->_redirect('/account/login');
			}
		}
		else {
			$infoUser = $auth->getIdentity();
			$customer_id = $infoUser['user_id'];
			
			if ($customer_id !='KH001')
				$this->_redirect('/index/index');
		}
	}
      
	public function customerAction()
    {
        $sql = "select * from customer";    	
        $connect = new Application_Model_ConnectDB();
    	$data = $connect->dbTable($sql);
    	$this->view->data = $data; 
    }    
	public function addAction()
    {
    	if(isset($_POST['insert']))
    	{
        	$item_id = $this->_request->getParam('item_id');        
       		$item_name = $this->_request->getParam('item_name');
       		$kind = $this->_request->getParam('kind');      		
       		$description = $this->_request->getParam('description');
       		$file = $_FILES['file']['name'];
       		$price = $this->_request->getParam('price');       		       		
       		$location = "C:\\wamp\\www\\Zend\\public\\images\\front-end\\";
       		$destination = $location. $_FILES['file']['name'];       		     		
       		move_uploaded_file($_FILES['file']['tmp_name'],$destination);
       		$data = array('item_id' => $item_id,
       				  'item_name' => $item_name,
       				  'kind' => $kind,
       				  'description' => $description,
       				  'images' => $file,
       				  'price' => $price);
       		$connect = new Application_Model_ConnectDB(); 
       		$connect->dbConnect()->insert('items', $data);
       		$this->_redirect('/admin/menu');
    	}
    }
    
	public function menuAction()
    {
        $sql = "select * from items";
        $connect = new Application_Model_ConnectDB();
        $data = $connect->dbTable($sql);
        $this->view->data = $data; 
    }
    
	public function revenueAction()
    {
    	$month = date('m') - 1;
    	$query = "select * from bill";
        $sql = "select * from customer where MONTH(customer_regdate) = $month";       
        $connect = new Application_Model_ConnectDB();
        $listCustomer = $connect->dbTable($sql);
        $bills = $connect->dbTable($query);
        $this->view->users = $listCustomer;
        $this->view->bills = $bills;
    }
	public function galleryAction()
    {
        // action body
    }
    
	public function editAction()
    { 
    	if(isset($_POST['update']))
    	{
    		$item_id = $this->_request->getParam('item_id');        
       		$item_name = $this->_request->getParam('item_name');
       		$kind = $this->_request->getParam('kind');      		
       		$description = $this->_request->getParam('description');
			$image= $this->_request->getParam('file');				
       		$price = $this->_request->getParam('price');       		       		
       		$location = "C:\\wamp\\www\\Zend\\public\\images\\front-end\\";
       		$destination = $location. $_FILES['file']['name'];       		     		
       		move_uploaded_file($_FILES['file']['tmp_name'],$destination);
       		$where = "item_id = '$item_id'";
			if(trim($_FILES['file']['tmp_name']) != ''){
				$file = $_FILES['file']['name'];	
				$data = array('item_id' => $item_id,
						  'item_name' => $item_name,
						  'kind' => $kind,
						  'description' => $description,
						  'images' => $file,
						  'price' => $price);
			} else 	$data = array('item_id' => $item_id,
						  'item_name' => $item_name,
						  'kind' => $kind,
						  'description' => $description,						  
						  'price' => $price);		
       		$connect = new Application_Model_ConnectDB();       	
       		$connect->dbConnect()->update('items',$data, $where);      		
       		$this->_redirect('admin/menu');    
    	} else{   
       		$item_id = $this->_request->getParam('item_id');
       		$sql = "select * from items where item_id = '$item_id'";
       		$connect = new Application_Model_ConnectDB();
       		$listItem = $connect->dbTable($sql);
       		$this->view->data = $listItem;
     	}
    }
	
    public function insertAction()
    {
        if(isset($_POST['insert']))
        {
        	$customer_id = $this->_request->getParam('userid');
    		$customer_name = $this->_request->getParam('username');
    		$customer_password = $this->_request->getParam('password');
    		$customer_regdate = $this->_request->getParam('regdate');
    		$current = $customer_regdate." ".date('H:m:s');
    		$data = array('customer_id' => $customer_id,
    				  'customer_name' => $customer_name,
    				  'customer_password' => $customer_password,
    				  'customer_regdate' => $current,
    				  'customer_last_login' => $current);
    		$connect = new Application_Model_ConnectDB();
    		$connect->dbConnect()->insert('customer', $data);
    		$this->_redirect('/admin/customer');
        }
    }  
    
    public function updateAction()
    {
    	if(isset($_POST['update']))
    	{
    		$customer_id = $this->_request->getParam('userid');
	    	$customer_name = $this->_request->getParam('username');
	    	$customer_password = $this->_request->getParam('password');
	    	$customer_regdate = $this->_request->getParam('regdate');
	    	$current = $customer_regdate." ".date('H:m:s');
	    	$data = array('customer_id' => $customer_id,
	    				  'customer_name' => $customer_name,
	    				  'customer_password' => $customer_password,
	    				  'customer_regdate' => $current,
	    				  'customer_last_login' => $current);
	    	$connect = new Application_Model_ConnectDB();
	    	$where = "customer_id = '$customer_id'";
	    	$connect->dbConnect()->update('customer', $data,$where);
	    	$this->_redirect('/admin/customer');
    	} else {
	       $customer_id = $this->_request->getParam('user_id');
	       $connect = new Application_Model_ConnectDB();
	       $sql = "select * from customer where customer_id = '$customer_id'";
	       $data = $connect->dbTable($sql);
	       $this->view->data = $data;
    	}
    }    
	
    public function deleteAction()
    {
       $customer_id = $this->_request->getParam('user_id');       
       $where = "customer_id='$customer_id'";
       $connect = new Application_Model_ConnectDB();
       $connect->dbConnect()->delete('customer',$where); 
       $this->_redirect('admin/customer');      
    }

    public function removeAction()
    {
    	$item_id = $this->_request->getParam('item_id');    	
    	$where = "item_id='$item_id'";
    	$sql = "select * from eatwith where item_id = '$item_id'";
    	$connect = new Application_Model_ConnectDB();
    	$listSpice = $connect->dbTable($sql);
    	for($i = 0;$i < count($listSpice);$i++)
    	{
    		$spice_id = $listSpice['$i']['spice_id'];
    		$query = "spice_id = '$spice_id'";
    		$connect->dbConnect()->delete('spices',$query);      		
    	}
    	$connect->dbConnect()->delete('eatwith',$where); 
    	$connect->dbConnect()->delete('items',$where);    	   	
    	$this->_redirect('/admin/menu');
    }

	
}
